package com.esd.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esd.common.pojo.PageResult;
import com.esd.common.pojo.R;
import com.esd.device.entity.Device;
import com.esd.device.entity.DeviceType;
import com.esd.device.mapper.DeviceMapper;
import com.esd.device.mapper.DeviceTypeMapper;
import com.esd.device.service.DeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.Objects;

@Component
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper,DeviceType> implements DeviceTypeService {

    @Override
    public R search(Map<String, Object> paramsMap) {
        Page<DeviceType> page = new Page<>((Integer) paramsMap.get("currentPage") , (Integer) paramsMap.get("pageSize")) ;
        LambdaQueryWrapper<DeviceType> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(paramsMap.get("queryString"))){
            queryWrapper.like(DeviceType::getDeviceTypeName, paramsMap.get("queryString").toString().trim());
        }
        Page<DeviceType> deviceTypePage = this.page( page , queryWrapper);
        return R.ok(new PageResult(deviceTypePage.getTotal(),deviceTypePage.getRecords()));
    }

    @Override
    public R addDeviceType(DeviceType deviceType) {
        LambdaQueryWrapper<DeviceType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceType::getDeviceTypeName, deviceType.getDeviceTypeName());
        DeviceType deviceType1 = baseMapper.selectOne(queryWrapper,false);
        if (deviceType1!= null){
            return R.error("该终端类型已存在，请查询编辑！！");
        }
        baseMapper.insert(deviceType);
        return R.ok("添加终端类型成功！！");
    }

    @Override
    public R updateDeviceType(DeviceType deviceType) {
        LambdaQueryWrapper<DeviceType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceType::getDeviceTypeName, deviceType.getDeviceTypeName());
        DeviceType deviceType1 = baseMapper.selectOne(queryWrapper , false);
        if (deviceType1!= null && !Objects.equals(deviceType1.getDeviceTypeId(), deviceType.getDeviceTypeId())){
            return R.error("该终端类型已存在，请查询编辑！！");
        }
        baseMapper.updateById(deviceType) ;
        return R.ok("编辑终端类型成功！！");
    }


    @Autowired
    private DeviceMapper deviceMapper ;

    @Transactional
    @Override
    public R deleteById(Long deviceTypeId) {
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDeviceTypeId, deviceTypeId);
        Device device = deviceMapper.selectOne(queryWrapper,false);
        if (device != null) {
            return R.error("该终端类型下存在关联的终端！不能删除。");
        }
        baseMapper.deleteById(deviceTypeId);
        return R.ok("删除终端类型成功！！");
    }

    @Override
    public R selectById(Long deviceTypeId) {
        DeviceType deviceType = baseMapper.selectById(deviceTypeId);
        return R.ok(deviceType);
    }

}
